package com.greenantmall.activity;

import java.io.File;
import java.io.FileOutputStream;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.socialization.Socialization;

import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.AntHomeApplication;
import com.greenantmall.utils.NotificationUtil;
import com.greenantmall.utils.SettingUtils;
import com.greenantmall.utils.ShareUtil;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.view.CommonDialog;
import com.greenantmall.view.CommonLoadingAndErrorDialog;
import com.lsarah.utils.SystemBarTintManager;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class SettingsActivity extends BaseActivity
{
    private CheckBox allow_notification;
    private CheckBox notification_sound;
    private Button btn_login;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    private TextView tele;
    private Context context;
    private String teleP = "";
    


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        SystemBarTintManager.setContentView(this, R.layout.activity_settings, getResources().getColor(R.color.green));
        context = this;
        allow_notification = (CheckBox) findViewById(R.id.allow_notification);
        notification_sound = (CheckBox) findViewById(R.id.notification_sound);
        tele = (TextView) findViewById(R.id.tele);
        btn_login = (Button) findViewById(R.id.btn_login);
        allow_notification.setChecked(SettingUtils.isAllowNotification(this,
            true));
        notification_sound.setChecked(SettingUtils.isAllowNotificationSound(
            this, false));
        String teleP = "";
        try
        {
            teleP = String.valueOf(AntHomeApplication.serviePhone.getPhone());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(teleP))
        {
            tele.setText(this.getString(R.string.settings_wifi_hint,
                teleP));
        } else
        {
            findViewById(R.id.teleView).setVisibility(View.GONE);
        }
        
		
    }

  
    public void goBack(View view)
    {
        finish();
    }

    public void addressManage(View view)
    {
        if (UtilTools.isUserLogined(this))
        {
            Intent intent = new Intent(this, MyAddressActivity.class);
            startActivity(intent);
        }
    }

    public void makeaCall(View view)
    {

        try
        {
            teleP = String.valueOf(AntHomeApplication.serviePhone.getPhone());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(teleP))
        {

            CommonDialog errDialog = new CommonDialog(
                context,
                R.style.ExitDialogStyle,
                context.getString(R.string.common_title),
                context.getString(R.string.orderdetail_call));
            errDialog
                .setSureListener(new CommonDialog.OnSureClickListener()
                {

                    @Override
                    public void sureClicked()
                    {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                            + teleP));
                        startActivity(intent);
                    }
                });
            errDialog.show();
        }

    }

    public void promiseService(View view)
    {

        Intent intent = new Intent(this, DisclaimerActivity.class);
        startActivity(intent);

    }
    


    public void cleanAllCache(View view)
    {
        final CommonLoadingAndErrorDialog loading = new CommonLoadingAndErrorDialog(
            this);
        loading.show();
        UtilTools.cleanAllCache(this);
        mHandler.postDelayed(new Runnable()
        {

            @Override
            public void run()
            {
                loading.dismiss();

            }
        }, 1500);

    }

    public void logout(View view)
    {
        if (UtilTools.isUserLogined(this))
        {
            NotificationUtil.unRegisterNotificationWhenUserChange(this);
            AVUser.logOut();
            UtilTools.showMyToast(this, this.getString(R.string.success));
            btn_login.setText(R.string.activity_login_login);
        }

    }

    @Override
    protected void onResume()
    {
        super.onResume();

        AVUser currentUser = AVUser.getCurrentUser();
        if (currentUser != null)
        {
            btn_login.setText(R.string.settings_logout);
        } else
        {
            btn_login.setText(R.string.activity_login_login);
        }
    }

    public void showAbout(View view)
    {
        Intent intent = new Intent(this, AboutActivity.class);
        startActivity(intent);
    }

    public void allowNotification(View view)
    {
        SettingUtils.setAllowNotification(this, allow_notification.isChecked());
    }

    public void allowNotificationSound(View view)
    {
        SettingUtils.setAllowNotificationSound(this,
            notification_sound.isChecked());

        if (AntHomeApplication.soundPool != null)
        {
            AntHomeApplication.soundPool.play(1, 1, 1, 0, 2, 1);
        }
    }

}
